package com.example;


import com.example.utils.Invoke;
import com.example.utils.InvokeTable;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
public class AnnotationScanner implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> beanClass = bean.getClass();
        if (beanClass.isAnnotationPresent(Module.class)) {
            Method[] methods = beanClass.getMethods();
            if (methods != null && methods.length > 0) {
                for (Method method : methods) {
                    if (method.isAnnotationPresent(Cmd.class)) {
                        Module module = beanClass.getAnnotation(Module.class);
                        Cmd cmd = method.getAnnotation(Cmd.class);
                        String moduleValue = module.module();
                        String cmdValue = cmd.cmd();
                        if (InvokeTable.getInvoke(moduleValue, cmdValue) == null) {
                            InvokeTable.addInvoke(moduleValue, cmdValue, Invoke.createInvoke(method, bean));
                        } else {
                            System.out.println("该模块下已经存在同样的缓存。。");
                        }
                    }
                }
            }
        }

        return null;
    }
}
